package fourmiefight.metier;

import org.dyn4j.geometry.Circle;

import fourmiefight.abstractionlib2d.CustomBody;
import fourmiefight.ihm.FoodView;

/**
 * Classe métier de nourriture
 * @author M1G2
 */
public class Food extends CustomBody
{
	// Constante(s)
	private static final int DEFAULT_FOOD = 50;
	private static final int QUANTITE_NOURRITURE = 10;
	private static final double MAX_WIDTH = 0.003;
	private static final double DIVIDER = 300000;
		
	private double maxSize = -1;
	
	// Permet de gérer l'autoincrément de l'id
	//static AtomicInteger nextId = new AtomicInteger();
	public static int currId = 0;
	
	// Attributs
	private int idd; // On ne met pas id car CustomBody possède déja un id
	private int quantityMax;
	private int quantityLeft;
	
	/**
	 * Construteur par défaut
	 */
	public Food() 
	{
		initialize(DEFAULT_FOOD);
	}
	
	/**
	 * Constructeur paramétré
	 * @param quantity
	 */
	public Food(int quantity)
	{
		initialize(quantity);
	}
	
	/**
	 * Méthode d'initialisation de l'objet partagée par les deux constructeurs
	 * @param quantity
	 */
	private void initialize(int quantity)
	{
		this.setIdd(/*nextId.incrementAndGet()*/currId);
		currId++;
		view = new FoodView();
		quantityMax = quantity;
		setQuantityLeft(quantity);
	}
	
	public void updateViewScale()
	{
		if(maxSize < 0)
		{
			maxSize = ((float)quantityMax) / DIVIDER;
			if(maxSize > MAX_WIDTH)
			{
				maxSize = MAX_WIDTH;
			}
		}
				
		this.removeAllFixtures();
		float percent = ((float)quantityLeft) / ((float)quantityMax) * 100.0f;
		Circle circle = new Circle(percent * maxSize);
        this.addFixture(circle);
        this.setMass();
	}
	
	/**
	 * Permet de placer la nourriture au bon endroit (paliatif à un bug)
	 * @param d
	 * @param e
	 */
	public void placeFoodView(double d, double e) 
	{
		this.translate(d, e);
	}
	
	public int decreaseFoodCount()
	{
		int remove = 0;
		if(QUANTITE_NOURRITURE < this.getQuantityLeft())
		{
			remove = QUANTITE_NOURRITURE;
		}
		else
		{
			remove = this.getQuantityLeft();
		}
		this.setQuantityLeft(this.getQuantityLeft() - remove);
		return remove;
	}

	// ---------- Getters && Setters ---------- //

	public int getQuantityMax() 
	{
		return quantityMax;
	}

	public int getQuantityLeft() 
	{
		return quantityLeft;
	}

	public void setQuantityLeft(int quantityLeft) 
	{
		this.quantityLeft = quantityLeft;
		if(quantityLeft > 0)
		{
			updateViewScale();
		}
		else
		{
			this.getWorld().removeBody(this);
		}
	}

	public int getIdd() {
		return idd;
	}

	public void setIdd(int idd) {
		this.idd = idd;
	}
	
	
}
